var z = Object.defineProperty;
var m = (p, e, i) =>
  e in p
    ? z(p, e, { enumerable: !0, configurable: !0, writable: !0, value: i })
    : (p[e] = i);
var a = (p, e, i) => (m(p, typeof e != 'symbol' ? e + '' : e, i), i);
import { _Tokenizer as d } from './Tokenizer.js';
import { _defaults as y } from './defaults.js';
import { block as x, inline as g } from './rules.js';
import './helpers.js';
class w {
  constructor(e) {
    a(this, 'tokens');
    a(this, 'options');
    a(this, 'state');
    a(this, 'tokenizer');
    a(this, 'inlineQueue');
    (this.tokens = []),
      (this.tokens.links = /* @__PURE__ */ Object.create(null)),
      (this.options = e || y),
      (this.options.tokenizer = this.options.tokenizer || new d()),
      (this.tokenizer = this.options.tokenizer),
      (this.tokenizer.options = this.options),
      (this.tokenizer.lexer = this),
      (this.inlineQueue = []),
      (this.state = {
        inLink: !1,
        inRawBlock: !1,
        top: !0,
      });
    const i = {
      block: x.normal,
      inline: g.normal,
    };
    this.options.pedantic
      ? ((i.block = x.pedantic), (i.inline = g.pedantic))
      : this.options.gfm &&
        ((i.block = x.gfm),
        this.options.breaks ? (i.inline = g.breaks) : (i.inline = g.gfm)),
      (this.tokenizer.rules = i);
  }
  /**
   * Expose Rules
   */
  static get rules() {
    return {
      block: x,
      inline: g,
    };
  }
  /**
   * Static Lex Method
   */
  static lex(e, i) {
    return new w(i).lex(e);
  }
  /**
   * Static Lex Inline Method
   */
  static lexInline(e, i) {
    return new w(i).inlineTokens(e);
  }
  /**
   * Preprocessing
   */
  lex(e) {
    (e = e.replace(
      /\r\n|\r/g,
      `
`,
    )),
      this.blockTokens(e, this.tokens);
    let i;
    for (; (i = this.inlineQueue.shift()); ) this.inlineTokens(i.src, i.tokens);
    return this.tokens;
  }
  blockTokens(e, i = []) {
    this.options.pedantic
      ? (e = e.replace(/\t/g, '    ').replace(/^ +$/gm, ''))
      : (e = e.replace(
          /^( *)(\t+)/gm,
          (l, u, o) => u + '    '.repeat(o.length),
        ));
    let t, n, r, s;
    for (; e; )
      if (
        !(
          this.options.extensions &&
          this.options.extensions.block &&
          this.options.extensions.block.some(l =>
            (t = l.call({ lexer: this }, e, i))
              ? ((e = e.substring(t.raw.length)), i.push(t), !0)
              : !1,
          )
        )
      ) {
        if ((t = this.tokenizer.space(e))) {
          (e = e.substring(t.raw.length)),
            t.raw.length === 1 && i.length > 0
              ? (i[i.length - 1].raw += `
`)
              : i.push(t);
          continue;
        }
        if ((t = this.tokenizer.code(e))) {
          (e = e.substring(t.raw.length)),
            (n = i[i.length - 1]),
            n && (n.type === 'paragraph' || n.type === 'text')
              ? ((n.raw +=
                  `
` + t.raw),
                (n.text +=
                  `
` + t.text),
                (this.inlineQueue[this.inlineQueue.length - 1].src = n.text))
              : i.push(t);
          continue;
        }
        if ((t = this.tokenizer.fences(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.heading(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.hr(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.blockquote(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.list(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.html(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.def(e))) {
          (e = e.substring(t.raw.length)),
            (n = i[i.length - 1]),
            n && (n.type === 'paragraph' || n.type === 'text')
              ? ((n.raw +=
                  `
` + t.raw),
                (n.text +=
                  `
` + t.raw),
                (this.inlineQueue[this.inlineQueue.length - 1].src = n.text))
              : this.tokens.links[t.tag] ||
                (this.tokens.links[t.tag] = {
                  href: t.href,
                  title: t.title,
                });
          continue;
        }
        if ((t = this.tokenizer.table(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.lheading(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if (
          ((r = e),
          this.options.extensions && this.options.extensions.startBlock)
        ) {
          let l = 1 / 0;
          const u = e.slice(1);
          let o;
          this.options.extensions.startBlock.forEach(h => {
            (o = h.call({ lexer: this }, u)),
              typeof o == 'number' && o >= 0 && (l = Math.min(l, o));
          }),
            l < 1 / 0 && l >= 0 && (r = e.substring(0, l + 1));
        }
        if (this.state.top && (t = this.tokenizer.paragraph(r))) {
          (n = i[i.length - 1]),
            s && n.type === 'paragraph'
              ? ((n.raw +=
                  `
` + t.raw),
                (n.text +=
                  `
` + t.text),
                this.inlineQueue.pop(),
                (this.inlineQueue[this.inlineQueue.length - 1].src = n.text))
              : i.push(t),
            (s = r.length !== e.length),
            (e = e.substring(t.raw.length));
          continue;
        }
        if ((t = this.tokenizer.text(e))) {
          (e = e.substring(t.raw.length)),
            (n = i[i.length - 1]),
            n && n.type === 'text'
              ? ((n.raw +=
                  `
` + t.raw),
                (n.text +=
                  `
` + t.text),
                this.inlineQueue.pop(),
                (this.inlineQueue[this.inlineQueue.length - 1].src = n.text))
              : i.push(t);
          continue;
        }
        if (e) {
          const l = 'Infinite loop on byte: ' + e.charCodeAt(0);
          if (this.options.silent) {
            console.error(l);
            break;
          } else throw new Error(l);
        }
      }
    return (this.state.top = !0), i;
  }
  inline(e, i = []) {
    return this.inlineQueue.push({ src: e, tokens: i }), i;
  }
  /**
   * Lexing/Compiling
   */
  inlineTokens(e, i = []) {
    let t,
      n,
      r,
      s = e,
      l,
      u,
      o;
    if (this.tokens.links) {
      const h = Object.keys(this.tokens.links);
      if (h.length > 0)
        for (
          ;
          (l = this.tokenizer.rules.inline.reflinkSearch.exec(s)) != null;

        )
          h.includes(l[0].slice(l[0].lastIndexOf('[') + 1, -1)) &&
            (s =
              s.slice(0, l.index) +
              '[' +
              'a'.repeat(l[0].length - 2) +
              ']' +
              s.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex));
    }
    for (; (l = this.tokenizer.rules.inline.blockSkip.exec(s)) != null; )
      s =
        s.slice(0, l.index) +
        '[' +
        'a'.repeat(l[0].length - 2) +
        ']' +
        s.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
    for (; (l = this.tokenizer.rules.inline.anyPunctuation.exec(s)) != null; )
      s =
        s.slice(0, l.index) +
        '++' +
        s.slice(this.tokenizer.rules.inline.anyPunctuation.lastIndex);
    for (; e; )
      if (
        (u || (o = ''),
        (u = !1),
        !(
          this.options.extensions &&
          this.options.extensions.inline &&
          this.options.extensions.inline.some(h =>
            (t = h.call({ lexer: this }, e, i))
              ? ((e = e.substring(t.raw.length)), i.push(t), !0)
              : !1,
          )
        ))
      ) {
        if ((t = this.tokenizer.escape(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.tag(e))) {
          (e = e.substring(t.raw.length)),
            (n = i[i.length - 1]),
            n && t.type === 'text' && n.type === 'text'
              ? ((n.raw += t.raw), (n.text += t.text))
              : i.push(t);
          continue;
        }
        if ((t = this.tokenizer.link(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.reflink(e, this.tokens.links))) {
          (e = e.substring(t.raw.length)),
            (n = i[i.length - 1]),
            n && t.type === 'text' && n.type === 'text'
              ? ((n.raw += t.raw), (n.text += t.text))
              : i.push(t);
          continue;
        }
        if ((t = this.tokenizer.emStrong(e, s, o))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.codespan(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.br(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.del(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if ((t = this.tokenizer.autolink(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if (!this.state.inLink && (t = this.tokenizer.url(e))) {
          (e = e.substring(t.raw.length)), i.push(t);
          continue;
        }
        if (
          ((r = e),
          this.options.extensions && this.options.extensions.startInline)
        ) {
          let h = 1 / 0;
          const b = e.slice(1);
          let f;
          this.options.extensions.startInline.forEach(k => {
            (f = k.call({ lexer: this }, b)),
              typeof f == 'number' && f >= 0 && (h = Math.min(h, f));
          }),
            h < 1 / 0 && h >= 0 && (r = e.substring(0, h + 1));
        }
        if ((t = this.tokenizer.inlineText(r))) {
          (e = e.substring(t.raw.length)),
            t.raw.slice(-1) !== '_' && (o = t.raw.slice(-1)),
            (u = !0),
            (n = i[i.length - 1]),
            n && n.type === 'text'
              ? ((n.raw += t.raw), (n.text += t.text))
              : i.push(t);
          continue;
        }
        if (e) {
          const h = 'Infinite loop on byte: ' + e.charCodeAt(0);
          if (this.options.silent) {
            console.error(h);
            break;
          } else throw new Error(h);
        }
      }
    return i;
  }
}
export { w as _Lexer };
